﻿using Hxj.Data;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Json;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RecruitStudents.Client.BLL.Impl
{
    public class BLLLottery
    {
        DAL.DALLottery dal = new DAL.DALLottery();
        public DataTable GetTB_Pad_SignedDataTable_New(string strExamPlaceNo, string strExamLevel, string strSpecialityKindsNO,
            string strExamYear, string strIdCard, string strName)
        {
            return dal.GetTB_Pad_SignedDataTable_New(strExamPlaceNo, strExamLevel, strSpecialityKindsNO, strExamYear, strIdCard, strName);
        }
        public DataTable CheckTB_Pad_Signed_New(string SpecialityKindsNO, string ExamLevel, string ExamArea, string idCard, string strExamYear)
        {
            return dal.CheckTB_Pad_Signed_New(SpecialityKindsNO, ExamLevel, ExamArea, idCard, strExamYear);
        }

        public JsonMessage MakeLottery_New(string SignUpID, string examinationID, string lotNo, string strExamPlaceNo, string strExamLevel, string strSpecNo, string strExamYear)
        {
            JsonMessage message = new JsonMessage();
            List<TB_Exam_SignUpStudentsSpeciality> list = dal.GetList<TB_Exam_SignUpStudentsSpeciality>(
                TB_Exam_SignUpStudentsSpeciality._.SignUpID == SignUpID
                && TB_Exam_SignUpStudentsSpeciality._.ExamLevel == strExamLevel
                && TB_Exam_SignUpStudentsSpeciality._.ExamPlaceNO == strExamPlaceNo
                && TB_Exam_SignUpStudentsSpeciality._.ExamYear == strExamYear
                && TB_Exam_SignUpStudentsSpeciality._.AllowExamID.IsNull("") != ""
                && "SpecialityKindsNO in (" + strSpecNo + ")"
                );
            List<TB_Exam_Entretenimientoh> listKW = dal.GetList<TB_Exam_Entretenimientoh>(
                TB_Exam_Entretenimientoh._.ExamPlaceNO == strExamPlaceNo
                && TB_Exam_Entretenimientoh._.ExamLevel == strExamLevel
                && TB_Exam_Entretenimientoh._.ExamYear == strExamYear
                && "SpecialityKindsNO in (" + strSpecNo + ")"
                );
            DateTime lotTime = DateTime.Now;
            List<TB_Exam_Signed> listSigned = new List<TB_Exam_Signed>();
            List<TB_Sys_ExamPlaceSpecialityKindsGroupMapping> listGroup = new List<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>();
            TB_Sys_ExamPlaceSpecialityKindsGroupMapping appointment = null;
            TB_Exam_Signed sign = null;
            foreach (TB_Exam_SignUpStudentsSpeciality model in list)
            {
                string strGroupNo = model.AppointmentGroup_First;
                appointment = dal.GetModel<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>(TB_Sys_ExamPlaceSpecialityKindsGroupMapping._.GroupNo == strGroupNo);
                if (appointment != null)
                {
                    sign = new TB_Exam_Signed();
                    sign.CreateTiime = lotTime;
                    sign.GroupNO = appointment.GroupNo;
                    sign.LotNO = lotNo;
                    sign.ExamYear = model.ExamYear;
                    sign.SignUpStudentsSpecialityID = model.SignUpStudentsSpecialityID;
                    foreach (TB_Exam_Entretenimientoh modelKW in listKW)
                    {
                        if (modelKW.SpecialityKindsNO == model.SpecialityKindsNO && modelKW.ExamPlaceNO == model.ExamPlaceNO && modelKW.ExamLevel == model.ExamLevel)
                        {
                            sign.EntretenimientohID = modelKW.EntretenimientohID;
                        }
                    }
                    if (string.IsNullOrEmpty(sign.EntretenimientohID))
                    {
                        message.State = "false";
                        message.Msg = "抽签失败：没有找到对应的考务数据！";
                        return message;
                    }
                    if (string.IsNullOrEmpty(appointment.NORecord))
                    {
                        appointment.NORecord = lotNo;
                    }
                    else
                    {
                        appointment.NORecord = appointment.NORecord + "," + lotNo;
                    }
                    listSigned.Add(sign);
                    listGroup.Add(appointment);
                }
            }
            DbTrans trans = DbHelper.GetDatabase().BeginTransaction();
            try
            {
                foreach (TB_Sys_ExamPlaceSpecialityKindsGroupMapping appointmentModel in listGroup)
                {
                    dal.Save<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>(appointmentModel, trans);
                }
                foreach (TB_Exam_Signed signModel in listSigned)
                {
                    dal.Save<TB_Exam_Signed>(signModel, trans);
                }
                trans.Commit();
                message.State = "true";
                message.Msg = "抽签成功";
            }
            catch (Exception ex)
            {
                trans.Rollback();
                message.State = "false";
                message.Msg = "抽签失败：" + ex.Message;
            }
            finally
            {
                trans.Close();
            }
            return message;
        }



        public DataTable GetTB_Pad_SignedDataTable(string strGroupNO, string strExamLevel, string strSpecialityKindsNO)
        {
            return dal.GetTB_Pad_SignedDataTable(strGroupNO, strExamLevel, strSpecialityKindsNO);
        }

        public DataTable CheckTB_Pad_Signed(string SpecialityKindsNO, string ExamLevel, string ExamArea, string idCard)
        {
            return dal.CheckTB_Pad_Signed(SpecialityKindsNO, ExamLevel, ExamArea, idCard);
        }

        public JsonMessage MakeLottery(string guid, string appointmentgroupid, string examinationID, string lotNo)
        {
            JsonMessage message = new JsonMessage();
            TB_Sys_ExamPlaceSpecialityKindsGroupMapping appointment = dal.GetModel<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>(TB_Sys_ExamPlaceSpecialityKindsGroupMapping._.GroupNo == appointmentgroupid);
            TB_Exam_SignUpStudentsSpeciality model = dal.GetModel<TB_Exam_SignUpStudentsSpeciality>(TB_Exam_SignUpStudentsSpeciality._.SignUpStudentsSpecialityID == guid);
            if (appointment != null && model != null)
            {
                TB_Exam_Signed sign = new TB_Exam_Signed();
                sign.CreateTiime = DateTime.Now;
                sign.EntretenimientohID = examinationID;
                sign.GroupNO = appointment.GroupNo;
                sign.LotNO = lotNo;
                sign.ExamYear = model.ExamYear;
                sign.SignUpStudentsSpecialityID = model.SignUpStudentsSpecialityID;
                if (string.IsNullOrEmpty(appointment.NORecord))
                {
                    appointment.NORecord = lotNo;
                }
                else
                {
                    appointment.NORecord = appointment.NORecord + "," + lotNo;
                }

                DbTrans trans = DbHelper.GetDatabase().BeginTransaction();
                try
                {
                    dal.Save<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>(appointment, trans);
                    dal.Save<TB_Exam_Signed>(sign, trans);
                    trans.Commit();
                    message.State = "true";
                    message.Msg = "抽签成功";
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    message.State = "false";
                    message.Msg = "抽签失败：" + ex.Message;
                }
                finally
                {
                    trans.Close();
                }
            }
            else
            {
                message.State = "false";
                message.Msg = "无分组信息！";
            }
            return message;
        }

        public JsonMessage CancelLottery(string strGroupNo, string signUpStudentsSpecialityId, string strSignedID)
        {
            JsonMessage message = new JsonMessage();
            TB_Sys_ExamPlaceSpecialityKindsGroupMapping appointment = dal.GetModel<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>(TB_Sys_ExamPlaceSpecialityKindsGroupMapping._.GroupNo == strGroupNo);
            TB_Exam_Signed model = dal.GetModel<TB_Exam_Signed>(TB_Exam_Signed._.SignedID == strSignedID);
            if (model != null)
            {
                string strNORecord = string.Empty;
                string strLotNo = model.LotNO;
                if (appointment != null)
                {
                    appointment.Attach();
                    string strLotRecord = appointment.NORecord;
                    string[] strSZ = strLotRecord.Split(',');
                    foreach (string strLot in strSZ)
                    {
                        if (strLot != strLotNo)
                        {
                            if (strNORecord.Length == 0)
                            {
                                strNORecord = strLot;
                            }
                            else
                            {
                                strNORecord = strNORecord + "," + strLot;
                            }
                        }
                    }
                    appointment.NORecord = strNORecord;
                }
                DbTrans trans = DbHelper.GetDatabase().BeginTransaction();
                try
                {
                    if (appointment != null)
                    {
                        DbHelper.GetDatabase().Update<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>(appointment, trans);
                    }
                    dal.Delete<TB_Exam_Signed>(TB_Exam_Signed._.SignedID == strSignedID, trans);
                    trans.Commit();
                    message.State = "true";
                    message.Msg = "操作成功!";
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    message.State = "false";
                    message.Msg = "操作失败," + ex.Message;
                }
                finally
                {
                    trans.Close();
                }
            }
            return message;
        }

        public DataTable GetPad_QueueDataTable(string stExamRoomID, string appointmentgroupid)
        {
            var where = TB_Pad_Queue._.ExamRoomNO == stExamRoomID && TB_Pad_Queue._.GroupNO == appointmentgroupid;

            return DbHelper.GetDatabase().From<TB_Pad_Queue>() //选择表
                .LeftJoin<TB_Exam_Signed>(TB_Pad_Queue._.SignedID == TB_Exam_Signed._.SignedID)
                                         .Select(TB_Pad_Queue._.All)  //设置选择的字段数目 
                                         .Where(where)   //条件
                                         .OrderBy(new OrderByClip(TB_Pad_Queue._.LotNO, OrderByOperater.ASC))   //排序
                                         .ToDataTable();   //返回List
        }

        public DataTable GetPad_Queue_PrintDataTable(string ExamLevel, string stExamRoomID, string strGroupNO)
        {
            var where = TB_Pad_Queue._.ExamRoomNO == stExamRoomID && TB_Exam_Signed._.GroupNO == strGroupNO;
            return dal.GetPad_Queue_PrintDataTable(ExamLevel, where);
        }

        public JsonMessage MakeExamRoomQueue(string strJson)
        {
            List<TB_Pad_Queue> list = JsonHelper.JsonToObject<List<TB_Pad_Queue>>(strJson);
            JsonMessage message = new JsonMessage();
            DbTrans trans = DbHelper.GetDatabase().BeginTransaction();
            try
            {
                if (list != null && list.Count > 0)
                {
                    foreach (TB_Pad_Queue model in list)
                    {
                        dal.Save<TB_Pad_Queue>(model, trans);
                        dal.ExecuteNonQuery("Update TB_Exam_Signed set State=0 where SignedID='" + model.SignedID + "'", trans);
                    }
                }
                trans.Commit();
                message.State = "true";
                message.Msg = "操作成功!";
            }
            catch (Exception ex)
            {
                trans.Rollback();
                message.State = "false";
                message.Msg = "操作失败," + ex.Message;
            }
            finally
            {
                trans.Close();
            }
            return message;
        }

        public JsonMessage CancelExamRoomQueue(string strQueueID)
        {
            JsonMessage message = new JsonMessage();
            TB_Pad_Queue model = dal.GetModel<TB_Pad_Queue>(TB_Pad_Queue._.QueueID == strQueueID);
            if (model != null)
            {
                model.Attach();
            }
            DbTrans trans = DbHelper.GetDatabase().BeginTransaction();
            try
            {
                dal.Delete<TB_Pad_Queue>(TB_Pad_Queue._.QueueID == strQueueID, trans);
                if (model != null)
                {
                    dal.ExecuteNonQuery("Update TB_Exam_Signed set State=1 where SignedID='" + model.SignedID + "'", trans);
                }
                trans.Commit();
                message.State = "true";
                message.Msg = "取消成功!";
            }
            catch (Exception ex)
            {
                trans.Rollback();
                message.State = "false";
                message.Msg = "取消失败," + ex.Message;
            }
            finally
            {
                trans.Close();
            }
            return message;
        }

        public DataTable GetAppointmentGroupDataTable(string strExamPlaceID, string strSpecialityKindsNO, string strExamLevel)
        {
            string[] strSZ = strSpecialityKindsNO.Split(',');
            string strSpec = string.Empty;
            foreach (string strData in strSZ)
            {
                if (strSpec.Length == 0)
                {
                    strSpec = "'" + strData + "'";
                }
                else
                {
                    strSpec += ",'" + strData + "'";
                }
            }
            string strWhere = "ExamPlaceNO='" + strExamPlaceID + "' and ExamLevel='" + strExamLevel + "' and SpecialityKindsNO in (" + strSpec + ")";
            string strWhereExamLevel = string.Empty;
            if (strExamLevel == "1")
            {
                strWhereExamLevel = "AppointmentGroup_First = T1.GroupNo and ISNULL(IsFee_First,0)=1";
            }
            else if (strExamLevel == "2")
            {
                strWhereExamLevel = "AppointmentGroup_Second = T1.GroupNo and ISNULL(IsFee_Second,0)=1";
            }
            else if (strExamLevel == "3")
            {
                strWhereExamLevel = "AppointmentGroup_Third = T1.GroupNo and ISNULL(IsFee_Third,0)=1";
            }
            else if (strExamLevel == "4")
            {
                strWhereExamLevel = "AppointmentGroup_Fourth = T1.GroupNo and ISNULL(IsFee_Fourth,0)=1";
            }
            else if (strExamLevel == "5")
            {
                strWhereExamLevel = "AppointmentGroup_Fifth = T1.GroupNo and ISNULL(IsFee_Fifth,0)=1";
            }
            else if (strExamLevel == "6")
            {
                strWhereExamLevel = "AppointmentGroup_Sixth = T1.GroupNo and ISNULL(IsFee_Sixth,0)=1";
            }
            return dal.GetAppointmentGroupDataTable(strWhere, strWhereExamLevel);
        }
    }
}
